package com.example.hbasespringbootdemo.dao;

import com.example.hbasespringbootdemo.model.City;
import com.spring4all.spring.boot.starter.hbase.api.RowMapper;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;

//实现 RowMapper 进行列簇转换
public class CityRowMapper implements RowMapper<City> {

    private static byte[] COLUMN_FAMILY = "info".getBytes();
    private static byte[] NAME = "name".getBytes();
    private static byte[] AGE = "age".getBytes();
    private static byte[] ID = "id".getBytes();

    @Override
    public City mapRow(Result result, int rowNum) throws Exception {
        String name = Bytes.toString(result.getValue(COLUMN_FAMILY, NAME));
        Integer age = Integer.valueOf(Bytes.toString(result.getValue(COLUMN_FAMILY, AGE)));
        Long id = Long.parseLong(Bytes.toString(result.getValue(COLUMN_FAMILY, ID)));
        City dto = new City();
        dto.setCityName(name);
        dto.setAge(age);
        dto.setId(id);
        return dto;
    }
}
